import 'dart:collection';

import 'package:flut_attendance_manage/attendance_manager.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<void> _incrementCounter() async {
    ///模拟打卡
    await AttendanceManager.instance.addClock();

    ///模拟移除某次打卡
    // for (var entry in AttendanceManager.instance.clockTable.entries) {
    //   if (entry.key == "2021-01-14" && entry.value.isNotEmpty) {
    //     await AttendanceManager.instance.removeClock(entry.value.first);
    //     break;
    //   }
    // }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              "打卡统计结果\n",
              style: TextStyle(fontSize: 30),
            ),
            Text(
              AttendanceManager.instance.getStatisticsInfoText(),
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: '打卡',
        child: const Text("打卡"),
      ),
    );
  }

  @override
  void initState() {
    ///页面初始化一些数据(模拟打卡，补卡)
    AttendanceManager.instance.addClock(date: DateTime(2021, 1, 13));
    AttendanceManager.instance.addClock(date: DateTime(2021, 1, 13));
    AttendanceManager.instance.addClock(date: DateTime(2021, 1, 14));
    AttendanceManager.instance.addClock(date: DateTime(2021, 12, 14));
    AttendanceManager.instance.addClock(date: DateTime(2021, 1, 13));
    AttendanceManager.instance.addClock(date: DateTime(2021, 1, 15));
  }
}
